<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\News;
use Illuminate\Http\Request;

class NewsController extends AdminController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'news';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-新闻案例管理';
    }
    public function index(Request $request){
        $page = $request->get('page', 1);
        $search = $request->all();
        $this->_response['_active']['_action']      = 'list';

        $NewsModel = new News();
        $data = $NewsModel->getData($search, $page, 20);
        $this->_response['news'] = $data;
        $this->_response['news']['pageData']['page'] = $page;
        $this->_response['news']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['news']['pageData']['lastPage'] , '/admin/news?');

        return view('admin.news.list', $this->_response);
    }

    //具体新闻 传入： id 返回
    public function getdata($id){
        $search['id']=$id;
        $NewsModel = new News();
        $data= $NewsModel->getData($search,1, 20);
        $this->_response['news'] = $data;
        if(!empty($data)){
            return response()->json(['code' => 0, 'msg' => '返回成功','data'=>$this->_response['news']]);

        }else{
            return response()->json(['code' => -1, 'msg' => '返回失败']);
        }
    }

    public function add(){
        $this->_response['_active']['_action']      = 'add';
        return view('admin.news.add', $this->_response);
    }

    public function doAdd(Request $request){
        $data = $request->all();
//        print_r($data);
//        exit;
        $saveData = [
            'backgroundImage' => Config('app.set.url').$data['img'],
            'title' => $data['title'],
            'article' => $data['article'],
            'invite_time' => date('Y-m-d H:i:s', time())
        ];

        $NewsModel = new News();
        if($NewsModel->saveData($saveData)){
            return response()->json(['code' => 0, 'msg' => '添加成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }
//添加新闻内容
    public function article($id){
        $this->_response['news'] = News::where('id', $id)->first();
        return view('admin.news.addarticle', $this->_response);
    }

    public function addarticle(Request $request){
        $data = $request->all();
//        print_r($data);
//        exit;
        $id =$data['id'];
        $setData = [

            'article' => $data['article'],
        ];
        $NewsModel = new News();
        if($NewsModel->setData($id,$setData)){
            return response()->json(['code' => 0, 'msg' => '编辑内容成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '编辑内容失败']);
        }
    }

    public function edit($id){
        $this->_response['news'] = News::where('id', $id)->first();
        return view('admin.news.edit', $this->_response);
    }

    public function doedit(\App\Http\Requests\News $request){
        $data = $request->all();
        $saveData =[
            'backgroundImage' =>Config('app.set.url').$data['img'],
            'title'=>$data['title'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];

        $NewsModel = new News();
        if($NewsModel->setData($data['id'],$saveData)){
            return response()->json(['code' => 0, 'msg' => '修改成功']);

        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

    public function delete($id){
        $NewsModel = new News();
        $data=[
            'deleted_at'=>date('Y-m-d H:i:s',time())
        ];
        if($NewsModel->delData($id,$data)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);

        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }

}